package com.example.yiwei.crm.view.activity.JYCoordingActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.TimePickerView;
import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.dao.dockDao;
import com.example.yiwei.crm.dao_impl.dockDaoImpl;
import com.example.yiwei.crm.data.GetAppointment;
import com.example.yiwei.crm.entity.ApointmentEntity;
import com.example.yiwei.crm.entity.BZFLEntity;
import com.example.yiwei.crm.entity.CustomerMessageEntity;
import com.example.yiwei.crm.entity.HospitalEntity;
import com.example.yiwei.crm.entity.KSEntity;
import com.example.yiwei.crm.entity.managerEntity;
import com.example.yiwei.crm.json.CoorCustomerJson;
import com.example.yiwei.crm.json.YYSpinnerJson;
import com.example.yiwei.crm.util.NetWorkUtils;
import com.example.yiwei.crm.util.SC;
import com.example.yiwei.crm.view.Class.listviewClass.CoorSelectAdapter;
import com.example.yiwei.crm.view.Class.listviewClass.CustomerSelectAdapter;
import com.example.yiwei.crm.view.activity.jtCustomer.JDcoorActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class JYAddApoActivity extends AppCompatActivity {
    private Spinner spinner_hospital,spinner_department,spinner_qdfl,spinner_qdly,
            spinner_disease_category,spinner_disease;
    private EditText edit_additional;
    private TextView txt_appointmen_date;
    private Button btn_show;
    private TimePickerView mpv;
    String appointment_date;
    List<String> hospital_list,manager_list,department_list,disease_category_list,disease_list,qdfl_list,qdly_list,jd_list;
    String result;
    String hospital_name,hospital_bianma,department_name,department_bianma,disease_category,disease,disease_bianma,qdfl,qdly;
    int hospital_id,department_id,disease_category_id,disease_id,qdfl_id,qdly_id;
    int qdfl1,qdly1;

    int qdfl_id1,qdly_id1;
    private ArrayAdapter adapter ,adapter2,adapter3,adapter4,adapter5,adapter6,adapter7,adapter8,adapter9,adapter10;
    private SharedPreferences sp;
    private List<HospitalEntity> list1;
    private List<KSEntity> list2;
    private List<BZFLEntity> list4;
    private List<BZFLEntity> list3;
    private List<BZFLEntity> list5;
    private CustomerMessageEntity customer;
    private List<BZFLEntity> list6;
    private List<managerEntity> list7=new ArrayList<>();
    private managerEntity managerEntityfz;
    private managerEntity managerEntityjd;
    private ApointmentEntity apo;
    List<com.example.yiwei.crm.entity.dockingEntity> coorlist= new ArrayList<>();
    private List<String> coor_name_list = new ArrayList<>();
    List<CustomerMessageEntity> customer_list = new ArrayList<>();
    List<String> customer_name_list = new ArrayList<>();
    int manager_id,jd_id,customer_id;
    String jd_name,manager_name,department_manager_name,deoartment_jd_name,coor_name,customer_name;
    Date date1,date2;
    private int A = 1,B = 1,i3 = 1,i4 = 1,i5 = 1,i6 = 1,i7 = 1,i8 = 1;
    private RelativeLayout coor_rl,customer_rl;
    Button button;
    private ImageView img_back;
    private TextView txt_title;
    private ImageView img_search,img_luru,img_me,img_tj;
    private ImageView img_coor_select;
    private PopupWindow mPopWindow,cPopWindow;
    private EditText edit_search,edit_seacher2;
    private Button btn_search,btn_search2;
    private ListView listView,listView2;
    private CoorSelectAdapter adapter1;
    private CustomerSelectAdapter adapterc;
    private TextView show_coor,show_customer;
    String coorname,customername;
    int coor_id;
    List<CustomerMessageEntity> clist;
    private ImageView img_select_customer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jyadd_apo);
        show_customer = (TextView) findViewById(R.id.show_customer_name);
        coor_rl = (RelativeLayout) findViewById(R.id.add_apo_coor_rl);
        img_select_customer = (ImageView) findViewById(R.id.img_xiangxia1);
        show_coor = (TextView) findViewById(R.id.show_coor_name);
        img_coor_select = (ImageView) findViewById(R.id.img_xiangxia);
        sp = getSharedPreferences("login", Context.MODE_PRIVATE);
        button = (Button) findViewById(R.id.add_apo_btn);
        img_back = (ImageView) findViewById(R.id.add_apo_img_back);
        img_search= (ImageView) findViewById(R.id.item_image_cx);
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        img_luru= (ImageView) findViewById(R.id.item_image_lr);
        img_tj= (ImageView) findViewById(R.id.item_image_tj);
        img_me= (ImageView) findViewById(R.id.item_image_w);
        img_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(JYAddApoActivity.this,JDcoorActivity.class);
                intent.putExtra("ii","ii");
                startActivity(intent);
                finish();
            }
        });
        img_luru.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(JYAddApoActivity.this,JDcoorActivity.class);
                startActivity(intent);
                finish();
            }
        });
        img_me.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(JYAddApoActivity.this,JDcoorActivity.class);
                intent.putExtra("me","me");
                startActivity(intent);
                finish();
            }
        });
        img_tj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(JYAddApoActivity.this,JDcoorActivity.class);
                intent.putExtra("tt","tt");
                startActivity(intent);
                finish();
            }
        });
        img_back.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        finish();
    }
});
        spinner_hospital= (Spinner) findViewById(R.id.customerApointmentMessage_spinner_hospital);
        spinner_department= (Spinner) findViewById(R.id.customerApointmentMessage_spinner_department);
//        spinner_doctor= (Spinner) findViewById(R.id.customerApointmentMessage_spinner_doctor);
        spinner_qdfl= (Spinner) findViewById(R.id.customerApointmentMessage_channel_classification);
        spinner_qdly= (Spinner) findViewById(R.id.customerApointmentMessage_channel_source);
        spinner_disease_category= (Spinner) findViewById(R.id.customerApointmentMessage_spinner_disease_category);
        spinner_disease= (Spinner) findViewById(R.id.customerApointmentMessage_spinner_disease);
        edit_additional= (EditText) findViewById(R.id.customerApointmentMessage_edit_additional_remarks);
        txt_appointmen_date= (TextView) findViewById(R.id.customerApointmentMessage_tv_time);
        txt_title= (TextView) findViewById(R.id.healt_education_add_txt_title);
        customer_rl = (RelativeLayout) findViewById(R.id.customer_sp_rl);
        if ("updata".equals(getIntent().getStringExtra("TAG"))){
            txt_title.setText("修改预约信息");
        }else{
            txt_title.setText("新增预约信息");
        }
        //修改时不显示合作客户
        if ("updata".equals(getIntent().getStringExtra("TAG"))){
            coor_rl.setVisibility(View.GONE);
            customer_rl.setVisibility(View.GONE);
        }
        //新增客户跳过来的不显示合作客户和客户
        if ("add".equals(getIntent().getStringExtra("tag"))){
            coor_rl.setVisibility(View.GONE);
            customer_rl.setVisibility(View.GONE);
            customer_id = getIntent().getIntExtra("KAA01",0);
        }
        btn_show= (Button) findViewById(R.id.customerApointmentMessage_btn_time);
        //创建选择器
        mpv = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        //设置时间范围
        Calendar calendar = Calendar.getInstance();
        if ("updata".equals(getIntent().getStringExtra("TAG"))){
            apo = GetAppointment.apoint;
        }
        mpv.setRange(calendar.get(Calendar.YEAR) - 100, calendar.get(Calendar.YEAR) + 100);
        //插入时间
        mpv.setTime(new Date());
        //选择监听
        mpv.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                appointment_date=getTime(date);
                txt_appointmen_date.setText(appointment_date);
            }
        });
        btn_show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mpv.show();
            }
        });
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                SC sc = new SC();
//                JSONObject object = sc.storage(sp);
//                String json2 = object.toString();
//                dockDao dockDao = new dockDaoImpl();
//                coorlist = dockDao.queryAllCoordinationMessage(baseClass.find_coordination_url + 1, json2);
//                runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        if (coorlist!=null){
//                            for (int i3 = 0;i3<coorlist.size();i3++){
//                                if (coorlist.get(i3).getDocking_mechanism()!=null&&!coorlist.get(i3).getDocking_mechanism().equals("")){
//                                    coor_name_list.add(coorlist.get(i3).getDocking_mechanism()+"-"+coorlist.get(i3).getDocking_name());
//                                }else{
//                                    coor_name_list.add(coorlist.get(i3).getDocking_name());
//                                }
//
//                            }
//                            adapter9 = new ArrayAdapter(JYAddApoActivity.this,R.layout.spinner_display_style,R.id.txtvwSpinner, coor_name_list);
//                            adapter9.setDropDownViewResource(R.layout.spinner_dropdown_style);
//                            spinner_coor.setAdapter(adapter9);
//                            spinner_coor.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
//                                @Override
//                                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//                                    coor_name = coorlist.get(position).getDocking_name();
//                                    coor_id = coorlist.get(position).getDocking_id();
//
//                                }
//
//                                @Override
//                                public void onNothingSelected(AdapterView<?> parent) {
//
//                                }
//                            });
//                        }
//                    }
//                });
//            }
//        }).start();
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                SC sc = new SC();
//                final JSONObject object = sc.storage(sp);
//                try {
//                    object.put("KXA01",coor_id);
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//                String json = object.toString();
//                String result = NetWorkUtils.getCount(baseClass.find_coor_allcustomer_URL+1,json);
//                customer_list = CoorCustomerJson.jsonVisitMessage(result);
//                runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        if (customer_list!=null){
//                            customer_name_list=new ArrayList<String>();
//                            for (int i=0;i<customer_list.size();i++){
//                                customer_name_list.add(customer_list.get(i).getClient_name()+"-"+customer_list.get(i).getSex()+"-"+customer_list.get(i).getClient_age());
//                            }
//                        }else{
//                            customer_name_list=new ArrayList<String>();
//
//                        }
//                        adapter10 = new ArrayAdapter(JYAddApoActivity.this,R.layout.spinner_display_style,R.id.txtvwSpinner,customer_name_list);
//                        adapter10.setDropDownViewResource(R.layout.spinner_dropdown_style);
//                        spinner_customer.setAdapter(adapter10);
//                        spinner_customer.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
//                            @Override
//                            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//                                customer_name = customer_name_list.get(position);
//                                if (customer_list!=null){
//                                    customer_id = customer_list.get(position).getClient_id();
//                                    qdfl1=customer_list.get(position).getChannel_classification_id();
//                                    qdly1=customer_list.get(position).getChannel_source_id();
//
//                                }
//
//                            }
//
//                            @Override
//                            public void onNothingSelected(AdapterView<?> parent) {
//
//                            }
//                        });
//                    }
//                });
//            }
//        }).start();
//        getqd();
        //选择合作客户
        img_coor_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        SC sc = new SC();
                        JSONObject object = sc.storage(sp);
                        String json = object.toString();
                        dockDao dockDao = new dockDaoImpl();
                        coorlist = dockDao.queryAllCoordinationMessage(baseClass.find_coordination_url + 1, json);
                        Log.d("ccc",coorlist.size()+"");
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (coorlist!=null){
                                    adapter1 = new CoorSelectAdapter(JYAddApoActivity.this,coorlist);
                                    showPopupWindow();
                                }
                            }
                        });
                    }
                }).start();
            }
        });
        //选择客户
        img_select_customer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (coor_id!=0){
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            SC sc = new SC();
                            final JSONObject object = sc.storage(sp);
                            try {
                                object.put("KXA01",coor_id);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            clist=null;
                            String json = object.toString();
                            String result = NetWorkUtils.getCount(baseClass.find_coor_allcustomer_URL+1,json);
                            clist = CoorCustomerJson.jsonVisitMessage(result);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (clist!=null){
                                        adapterc = new CustomerSelectAdapter(JYAddApoActivity.this,clist);
                                        showPopupWindow1();
                                    }else {
                                        Toast.makeText(JYAddApoActivity.this, "此合作伙伴没有患者", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                        }
                    }).start();
                }else {
                    Toast.makeText(JYAddApoActivity.this, "请选择合作伙伴", Toast.LENGTH_SHORT).show();
                }

            }
        });

        if ("add".equals(getIntent().getStringExtra("tag"))||"updata".equals(getIntent().getStringExtra("TAG"))){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    SC sc1 = new SC();
                    JSONObject object3 = sc1.storage(sp);
                    String json2 = object3.toString();
                    String result2 = NetWorkUtils.getCount(baseClass.find_QDFL_URL,json2);
                    list5 = YYSpinnerJson.getQDFL(result2);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            qdfl_list= new ArrayList<>();
                            qdfl_list.add("");
                            for (int i2=0;i2<list5.size();i2++){
                                qdfl_list.add(list5.get(i2).getName());
                            }
                            adapter5 = new ArrayAdapter(JYAddApoActivity.this,R.layout.spinner_display_style,R.id.txtvwSpinner,qdfl_list);
                            adapter5.setDropDownViewResource(R.layout.spinner_dropdown_style);
                            spinner_qdfl.setAdapter(adapter5);
                            spinner_qdfl.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                @Override
                                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                    if (position>0){
                                        qdfl=list5.get(position-1).getName();
                                        qdfl_id = list5.get(position-1).getId();
                                        i6++;
                                    }

                                    new Thread(new Runnable() {
                                        @Override
                                        public void run() {
                                            SC sc = new SC();
                                            JSONObject object2 = sc.storage(sp);
                                            try {
                                                object2.put("AKC01",qdfl_id);
                                            } catch (JSONException e) {
                                                e.printStackTrace();
                                            }
                                            String json = object2.toString();
                                            String result = NetWorkUtils.getCount(baseClass.find_QD_URL, json);
                                            list6 = YYSpinnerJson.getQDLY(result);
                                            runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    qdly_list = new ArrayList<String>();
                                                    qdly_list.add("");
                                                    for (int i2=0;i2<list6.size();i2++){
                                                        qdly_list.add( list6.get(i2).getName());
                                                    }
                                                    adapter6 = new ArrayAdapter(JYAddApoActivity.this,R.layout.spinner_display_style,R.id.txtvwSpinner,qdly_list);
                                                    adapter6.setDropDownViewResource(R.layout.spinner_dropdown_style);
                                                    spinner_qdly.setAdapter(adapter6);
                                                    spinner_qdly.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                                        @Override
                                                        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                                            if (position>0){
                                                                qdly=list6.get(position-1).getName();
                                                                qdly_id = list6.get(position-1).getId();
                                                            }

                                                        }
                                                        @Override
                                                        public void onNothingSelected(AdapterView<?> parent) {

                                                        }
                                                    });

                                                    if ("updata".equals(getIntent().getStringExtra("TAG"))){
                                                        apo = GetAppointment.apoint;
                                                        for (int i = 0;i<qdly_list.size();i++){
                                                            if (qdly_list.get(i).equals(apo.getClue_channel_source())){
                                                                spinner_qdly.setSelection(i);

                                                            }
                                                        }
                                                    }else if ("add".equals(getIntent().getStringExtra("tag"))){
                                                        for (int i = 0;i<qdly_list.size();i++){
                                                            if (qdly_list.get(i).equals(getIntent().getStringExtra("qdly"))){
                                                                spinner_qdly.setSelection(i);
                                                            }
                                                        }
                                                    }else {
                                                        for (int i = 0;i<list6.size();i++){
                                                            if (list6.get(i).getId()==qdly1){
                                                                spinner_qdly.setSelection(i+1);

                                                            }
                                                        }
                                                    }
                                                }
                                            });
                                        }
                                    }).start();
                                }
                                @Override
                                public void onNothingSelected(AdapterView<?> parent) {

                                }
                            });
                            if ("updata".equals(getIntent().getStringExtra("TAG"))) {
                                apo = GetAppointment.apoint;
                                for (int i = 0;i<qdfl_list.size();i++){
                                    if (qdfl_list.get(i).equals(apo.getClue_channel_classification())){
                                        spinner_qdfl.setSelection(i);
                                        i5++;
                                    }
                                }
                            }else if ("add".equals(getIntent().getStringExtra("tag"))){
                                for (int i = 0;i<qdfl_list.size();i++){
                                    if (qdfl_list.get(i).equals(getIntent().getStringExtra("qdfl"))){
                                        spinner_qdfl.setSelection(i);
                                    }
                                }
                            }else {
                                for (int i = 0;i<list5.size();i++){
                                    if (list5.get(i).getId()==qdfl1){
                                        spinner_qdfl.setSelection(i+1);
                                    }
                                }
                            }
                        }                });
                }
            }).start();
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                SC sc = new SC();
                JSONObject object = sc.storage(sp);
                try {
                    object.put("UAA04",1);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Log.d("cc",object.toString());
                String json = object.toString();

                String result = NetWorkUtils.getCount(baseClass.find_hospital_add_yy_URL, json);
                list1= YYSpinnerJson.getHospital(result);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        hospital_list= new ArrayList<>();
                        for (int i2=0;i2<list1.size();i2++){
                            hospital_list.add( list1.get(i2).getName());
                        }
                        adapter = new ArrayAdapter(JYAddApoActivity.this,R.layout.spinner_display_style,R.id.txtvwSpinner, hospital_list);
                        adapter.setDropDownViewResource(R.layout.spinner_dropdown_style);
                        spinner_hospital.setAdapter(adapter);
                        spinner_hospital.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                hospital_name=list1.get(position).getName();
                                hospital_bianma = list1.get(position).getBianma();
                                B++;
                                if ("updata".equals(getIntent().getStringExtra("TAG"))&&A==1){
                                    for (int i = 0;i<hospital_list.size();i++){
                                        if (hospital_list.get(i).equals(apo.getAppoint_hospital())){
                                            spinner_hospital.setSelection(i);
                                            A++;
                                        }
                                    }
                                }
                                if (null==hospital_name){

                                }else {
                                    new Thread(new Runnable() {
                                        @Override
                                        public void run() {
                                            SC sc = new SC();
                                            JSONObject object1 = sc.storage(sp);
                                            try {

                                                object1.put("UAA02", hospital_bianma);

                                            } catch (JSONException e) {
                                                e.printStackTrace();
                                            }
                                            String json = object1.toString();
                                            String result = NetWorkUtils.getCount(baseClass.find_KS_URL, json);
                                            list2 = YYSpinnerJson.getKS(result);
                                            runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    department_list = new ArrayList<String>();
                                                    for (int i2 = 0; i2 < list2.size(); i2++) {
                                                        department_list.add(list2.get(i2).getName());
                                                    }
                                                    adapter2 = new ArrayAdapter(JYAddApoActivity.this, R.layout.spinner_display_style, R.id.txtvwSpinner, department_list);
                                                    adapter2.setDropDownViewResource(R.layout.spinner_dropdown_style);
                                                    spinner_department.setAdapter(adapter2);
                                                    spinner_department.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                                        @Override
                                                        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                                            department_name = list2.get(position).getName();
                                                            department_bianma = list2.get(position).getBianma();

                                                        }

                                                        @Override
                                                        public void onNothingSelected(AdapterView<?> parent) {

                                                        }
                                                    });
                                                    if ("updata".equals(getIntent().getStringExtra("TAG")) && B == 2) {
                                                        System.out.print(department_list.toString() + "2252552555");
                                                        for (int i = 0; i < department_list.size(); i++) {
                                                            if (department_list.get(i).equals(apo.getAppint_department())) {
                                                                spinner_department.setSelection(i);
                                                                System.out.print("545465655");

                                                            }
                                                        }
                                                    }

                                                }
                                            });
                                        }
                                    }).start();
                                }
                            }
                            @Override
                            public void onNothingSelected(AdapterView<?> parent) {

                            }
                        });
                    }
                });
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                SC sc1 = new SC();
                JSONObject object3 = sc1.storage(sp);
                String json2 = object3.toString();
                String result2 = NetWorkUtils.getCount(baseClass.find_BZFL_URL,json2);
                list3 = YYSpinnerJson.getBZFL(result2);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        disease_category_list= new ArrayList<>();
                        disease_category_list.add("");
                        for (int i2=0;i2<list3.size();i2++){
                            disease_category_list.add( list3.get(i2).getName());
                        }
                        adapter3 = new ArrayAdapter(JYAddApoActivity.this,R.layout.spinner_display_style,R.id.txtvwSpinner,disease_category_list);
                        adapter3.setDropDownViewResource(R.layout.spinner_dropdown_style);
                        spinner_disease_category.setAdapter(adapter3);
                        spinner_disease_category.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                if (position>0){
                                    disease_category=list3.get(position-1).getName();
                                    disease_category_id = list3.get(position-1).getId();
                                    i4++;
                                }
                                if ("updata".equals(getIntent().getStringExtra("TAG"))&&i3==1){
                                    for (int i = 0;i<disease_category_list.size();i++){
                                        if (disease_category_list.get(i).equals(apo.getDisease_category())){
                                            spinner_disease_category.setSelection(i);
                                            i3++;
                                        }
                                    }
                                }if (null==disease_category){

                                }else {
                                    new Thread(new Runnable() {
                                        @Override
                                        public void run() {
                                            SC sc = new SC();
                                            JSONObject object2 = sc.storage(sp);
                                            try {

                                                object2.put("AKB01", disease_category_id);


                                            } catch (JSONException e) {
                                                e.printStackTrace();
                                            }
                                            Log.d("a", object2.toString());
                                            String json = object2.toString();
                                            Log.d("a", json);
                                            String result = NetWorkUtils.getCount(baseClass.find_BZ_URL, json);
                                            list4 = YYSpinnerJson.getBZ(result);
                                            runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    disease_list = new ArrayList<String>();
                                                    disease_list.add(0, "");
                                                    for (int i2 = 0; i2 < list4.size(); i2++) {
                                                        disease_list.add(list4.get(i2).getName());
                                                    }
                                                    adapter4 = new ArrayAdapter(JYAddApoActivity.this, R.layout.spinner_display_style, R.id.txtvwSpinner, disease_list);
                                                    adapter4.setDropDownViewResource(R.layout.spinner_dropdown_style);
                                                    spinner_disease.setAdapter(adapter4);
                                                    spinner_disease.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                                        @Override
                                                        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                                            if (position > 0) {
                                                                disease = list4.get(position - 1).getName();
                                                                disease_id = list4.get(position - 1).getId();
                                                            }

                                                        }

                                                        @Override
                                                        public void onNothingSelected(AdapterView<?> parent) {

                                                        }
                                                    });
                                                    if ("updata".equals(getIntent().getStringExtra("TAG")) && i4 == 2) {
                                                        for (int i = 0; i < disease_list.size(); i++) {
                                                            if (disease_list.get(i).equals(apo.getDisease())) {
                                                                spinner_disease.setSelection(i);

                                                            }
                                                        }
                                                    }
                                                }
                                            });
                                        }
                                    }).start();
                                }
                            }
                            @Override
                            public void onNothingSelected(AdapterView<?> parent) {

                            }
                        });
                    }
                });
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                SC sc = new SC();
                JSONObject object = sc.storage(sp);
                try {
                    object.put("admin",sp.getString("admin",null));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String json = object.toString();

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        manager_list=new ArrayList<String>();
                        jd_list = new ArrayList<String>();
                        for (int i=0;i<list7.size();i++){
                            jd_list.add(list7.get(i).getManager_name());
                            manager_list.add(list7.get(i).getManager_name());
                        }

                        adapter7 = new ArrayAdapter(JYAddApoActivity.this,R.layout.spinner_display_style,R.id.txtvwSpinner,manager_list);
                        adapter7.setDropDownViewResource(R.layout.spinner_dropdown_style);
//                        spinner_doctor.setAdapter(adapter7);
//                        spinner_doctor.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
//                            @Override
//                            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//                                if (position==0||position>0){
//                                    manager_id= list7.get(position).getManager_id();
//                                    manager_name=list7.get(position).getManager_name();
//                                    department_manager_name=list7.get(position).getDepartment();
//                                }
//
//                            }
//
//                            @Override
//                            public void onNothingSelected(AdapterView<?> parent) {
//
//                            }
//                        });
//                        if ("updata".equals(getIntent().getStringExtra("TAG"))&&i7==1){
//                            for (int i = 0;i<manager_list.size();i++){
//                                if (manager_list.get(i).equals(apo.getCurrent_create_name())){
//                                    spinner_doctor.setSelection(i);
//                                    i7++;
//                                }
//                            }
//                        }
                        adapter8 = new ArrayAdapter(JYAddApoActivity.this,R.layout.spinner_display_style,R.id.txtvwSpinner,jd_list);
                        adapter8.setDropDownViewResource(R.layout.spinner_dropdown_style);
                        jd_list.add(0,"");

                    }
                });
            }
        }).start();
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                add();
            }
        });
        if ("updata".equals(getIntent().getStringExtra("TAG"))){
            if (apo.getAppoint_date()!=null){
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//小写的mm表示的是分钟
                String dstr=apo.getAppoint_date();
                Date date= null;
                try {
                    date = sdf.parse(dstr);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                String s = getTime(date);
                txt_appointmen_date.setText(s);
            }
            edit_additional.setText(apo.getAppoint_additional());
        }
    }

    private void getqd() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                SC sc1 = new SC();
                JSONObject object3 = sc1.storage(sp);
                String json2 = object3.toString();
                String result2 = NetWorkUtils.getCount(baseClass.find_QDFL_URL,json2);
                list5 = YYSpinnerJson.getQDFL(result2);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        qdfl_list= new ArrayList<>();
                        qdfl_list.add("");
                        for (int i2=0;i2<list5.size();i2++){
                            qdfl_list.add(list5.get(i2).getName());
                        }
                        adapter5 = new ArrayAdapter(JYAddApoActivity.this,R.layout.spinner_display_style,R.id.txtvwSpinner,qdfl_list);
                        adapter5.setDropDownViewResource(R.layout.spinner_dropdown_style);
                        spinner_qdfl.setAdapter(adapter5);
                        spinner_qdfl.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                if (position>0){
                                    qdfl=list5.get(position-1).getName();
                                    qdfl_id = list5.get(position-1).getId();
                                    i6++;
                                }
                                    if (null==qdfl){

                                    }else {
                                        new Thread(new Runnable() {
                                            @Override
                                            public void run() {
                                                SC sc = new SC();
                                                JSONObject object2 = sc.storage(sp);
                                                try {
                                                    object2.put("AKC01", qdfl_id);
                                                } catch (JSONException e) {
                                                    e.printStackTrace();
                                                }
                                                String json = object2.toString();
                                                String result = NetWorkUtils.getCount(baseClass.find_QD_URL, json);
                                                list6 = YYSpinnerJson.getQDLY(result);
                                                runOnUiThread(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        qdly_list = new ArrayList<String>();
                                                        qdly_list.add("");
                                                        for (int i2 = 0; i2 < list6.size(); i2++) {
                                                            qdly_list.add(list6.get(i2).getName());
                                                        }
                                                        adapter6 = new ArrayAdapter(JYAddApoActivity.this, R.layout.spinner_display_style, R.id.txtvwSpinner, qdly_list);
                                                        adapter6.setDropDownViewResource(R.layout.spinner_dropdown_style);
                                                        spinner_qdly.setAdapter(adapter6);
                                                        spinner_qdly.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                                            @Override
                                                            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                                                if (position > 0) {
                                                                    qdly = list6.get(position - 1).getName();
                                                                    qdly_id = list6.get(position - 1).getId();
                                                                }

                                                            }

                                                            @Override
                                                            public void onNothingSelected(AdapterView<?> parent) {

                                                            }
                                                        });

                                                        if ("updata".equals(getIntent().getStringExtra("TAG"))) {
                                                            apo = GetAppointment.apoint;
                                                            for (int i = 0; i < qdly_list.size(); i++) {
                                                                if (qdly_list.get(i).equals(apo.getClue_channel_source())) {
                                                                    spinner_qdly.setSelection(i);

                                                                }
                                                            }
                                                        } else {
                                                            for (int i = 0; i < list6.size(); i++) {
                                                                if (list6.get(i).getId() == qdly1) {
                                                                    spinner_qdly.setSelection(i + 1);

                                                                }
                                                            }
                                                        }
                                                    }
                                                });
                                            }
                                        }).start();
                                    }
                            }
                            @Override
                            public void onNothingSelected(AdapterView<?> parent) {

                            }
                        });
                        if ("updata".equals(getIntent().getStringExtra("TAG"))) {
                            apo = GetAppointment.apoint;
                            Toast.makeText(JYAddApoActivity.this, apo.getClue_channel_classification(), Toast.LENGTH_SHORT).show();
                            for (int i = 0;i<qdfl_list.size();i++){
                                if (qdfl_list.get(i).equals(apo.getClue_channel_classification())){
                                    spinner_qdfl.setSelection(i);
                                    i5++;
                                }
                            }
                        }else{
                            for (int i = 0;i<list5.size();i++){
                                if (list5.get(i).getId()==qdfl1){
                                    spinner_qdfl.setSelection(i+1);
                                }
                            }
                        }
                    }                });
            }
        }).start();
    }

    public void add(){
        String a = getTime(new Date());
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

        SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟
        String dstr=txt_appointmen_date.getText().toString();
        try {
            date1=sdf1.parse(dstr);
            date2 =  formatter.parse(a);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (!"updata".equals(getIntent().getStringExtra("TAG"))&&!"add".equals(getIntent().getStringExtra("tag"))&&show_customer.getText().toString().equals("")&&show_coor.getText().equals("")){
            Toast.makeText(this, "请选择合作客户和患者", Toast.LENGTH_SHORT).show();
        }else {
            if (date1 != null && date2 != null) {
                if (!date1.before(date2)) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            SC sc = new SC();

                            JSONObject json = sc.storage(sp);
                            try {
                                if ("updata".equals(getIntent().getStringExtra("TAG"))) {
                                    json.put("KAA01", apo.getCustomer_id());
                                } else {
                                    json.put("KAA01", customer_id);
                                    json.put("KYA19", sp.getString("CID01", null));
                                    json.put("KYA20", sp.getString("CID03", null));
                                }
                                json.put("UAA02", hospital_bianma);
                                json.put("KYA02", txt_appointmen_date.getText().toString());
                                json.put("ADD02", department_bianma);
                                json.put("CID03", sp.getString("CID03", null));
                                json.put("AKB01", disease_category_id);
                                json.put("AKM01", disease_id);
                                json.put("KYA23", department_manager_name);
//                            json.put("KYA78",jd_id);
//                            json.put("KYA78A",jd_name);
                                json.put("KYA80", deoartment_jd_name);
                                json.put("AKC01", qdfl_id);
                                if ("updata".equals(getIntent().getStringExtra("TAG"))) {
                                    json.put("KYA01", apo.getAppoint_id());
                                }
                                json.put("AKD01", qdly_id);
                                json.put("KYA03", edit_additional.getText().toString());
                                String content = json.toString();
                                if ("updata".equals(getIntent().getStringExtra("TAG"))) {
                                    result = NetWorkUtils.getCount(baseClass.EDIT_YUYUE_URL, content);
                                } else {
                                    result = NetWorkUtils.getCount(baseClass.SAVE_YUYUE_URL, content);
                                }
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        try {
                                            JSONObject jsonObject = new JSONObject(result);
                                            boolean b = jsonObject.getBoolean("ifsuccess");
                                            if (b) {
                                                finish();
                                                if ("updata".equals(getIntent().getStringExtra("TAG"))) {
                                                    Toast.makeText(JYAddApoActivity.this, "修改预约成功", Toast.LENGTH_SHORT).show();
                                                } else {
                                                    Toast.makeText(JYAddApoActivity.this, "添加预约成功", Toast.LENGTH_SHORT).show();
                                                }
                                            } else {
                                                if (jsonObject.getString("msgs") != null) {
                                                    Toast.makeText(JYAddApoActivity.this, jsonObject.getString("msgs"), Toast.LENGTH_SHORT).show();
                                                } else {
                                                    Toast.makeText(JYAddApoActivity.this, "操作失败", Toast.LENGTH_SHORT).show();
                                                }
                                            }
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }

                                    }
                                });


                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }).start();
                } else {
                    Toast.makeText(this, "预约时间不能早于当前时间", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(this, "预约到院日期不能为空", Toast.LENGTH_SHORT).show();
            }
        }
    }
    public static String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }
    private void showPopupWindow() {
        View contentView = LayoutInflater.from(JYAddApoActivity.this).inflate(
                R.layout.popuwindoe_select_coor, null);

        mPopWindow = new PopupWindow(contentView);
        mPopWindow.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        mPopWindow.setHeight( LinearLayout.LayoutParams.WRAP_CONTENT);

        contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int popupWidth = contentView.getMeasuredWidth();    //  获取测量后的宽度
        final int popupHeight = contentView.getMeasuredHeight();  //获取测量后的高度
        int[] location = new int[2];
        edit_search = (EditText) contentView.findViewById(R.id.edit_seacher);
        btn_search = (Button) contentView.findViewById(R.id.btn_show);
        listView = (ListView) contentView.findViewById(R.id.btn_listview);
        listView.setAdapter(adapter1);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                coorname = coorlist.get(position).getDocking_name();
                coor_id = coorlist.get(position).getDocking_id();
                show_coor.setText(coorname);
                show_customer.setText("");
                customer_id=0;
                customername = null;
                mPopWindow.dismiss();
            }
        });
        btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        SC sc = new SC();
                        JSONObject object = sc.storage(sp);
                        try {
                            object.put("KXA02",edit_search.getText().toString());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        String json = object.toString();
                        dockDao dockDao = new dockDaoImpl();
                        coorlist = dockDao.queryAllCoordinationMessage(baseClass.find_coordination_url + 1, json);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (coorlist==null){
                                    Toast.makeText(JYAddApoActivity.this, "搜索不到信息！！", Toast.LENGTH_SHORT).show();
                                }else {
                                    adapter1 = new CoorSelectAdapter(JYAddApoActivity.this,coorlist);
                                    listView.setAdapter(adapter1);
                                }
                                adapter1.notifyDataSetChanged();
                            }
                        });
                    }
                }).start();
            }
        });
        img_coor_select.getLocationOnScreen(location);
        mPopWindow.setAnimationStyle(R.style.contextMenuAnim);
        mPopWindow.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.pop_bg));
        mPopWindow.setTouchable(true);
        mPopWindow.setOutsideTouchable(true);   //设置外部点击关闭ppw窗口
        mPopWindow.setFocusable(true);
        mPopWindow.update();
        mPopWindow.showAsDropDown(img_coor_select);
        backgroundAlpha(0.5f);
        mPopWindow.setOnDismissListener(new poponDismissListener());
    }
    private void showPopupWindow1() {
        View contentView = LayoutInflater.from(JYAddApoActivity.this).inflate(
                R.layout.popuwindow_select_customer, null);

        cPopWindow = new PopupWindow(contentView);
        cPopWindow.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        cPopWindow.setHeight( LinearLayout.LayoutParams.WRAP_CONTENT);

        contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int popupWidth = contentView.getMeasuredWidth();    //  获取测量后的宽度
        final int popupHeight = contentView.getMeasuredHeight();  //获取测量后的高度
        int[] location = new int[2];
        edit_seacher2 = (EditText) contentView.findViewById(R.id.edit_seacher);
        btn_search2 = (Button) contentView.findViewById(R.id.btn_show);
        listView2 = (ListView) contentView.findViewById(R.id.btn_listview);
        listView2.setAdapter(adapterc);
        listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                customer_name = clist.get(position).getClient_name();
                customer_id = clist.get(position).getClient_id();
                qdfl1=clist.get(position).getChannel_classification_id();
                qdly1=clist.get(position).getChannel_source_id();
                show_customer.setText(customer_name);
                getqd();
                cPopWindow.dismiss();
            }
        });
        btn_search2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        SC sc = new SC();
                        final JSONObject object = sc.storage(sp);
                        try {
                            object.put("KXA01",coor_id);
                            object.put("KAA02",edit_seacher2.getText().toString());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        String json = object.toString();
                        String result = NetWorkUtils.getCount(baseClass.find_coor_allcustomer_URL+1,json);
                        clist = CoorCustomerJson.jsonVisitMessage(result);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (clist==null){
                                    Toast.makeText(JYAddApoActivity.this, "搜索不到客户！！", Toast.LENGTH_SHORT).show();
                                }else {
                                    adapterc = new CustomerSelectAdapter(JYAddApoActivity.this,clist);
                                    listView2.setAdapter(adapterc);
                                }
                                adapterc.notifyDataSetChanged();
                            }
                        });
                    }
                }).start();
            }
        });
        img_select_customer.getLocationOnScreen(location);
        cPopWindow.setAnimationStyle(R.style.contextMenuAnim);
        cPopWindow.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.pop_bg));
        cPopWindow.setTouchable(true);
        cPopWindow.setOutsideTouchable(true);   //设置外部点击关闭ppw窗口
        cPopWindow.setFocusable(true);
        cPopWindow.update();
        cPopWindow.showAsDropDown(img_select_customer);
        backgroundAlpha(0.5f);
        cPopWindow.setOnDismissListener(new poponDismissListener());
    }
    public void backgroundAlpha(float bgAlpha)
    {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha= bgAlpha;
        getWindow().setAttributes(lp);
    }
    class poponDismissListener implements PopupWindow.OnDismissListener{

        @Override
        public void onDismiss() {
            // TODO Auto-generated method stub
            //Log.v("List_noteTypeActivity:", "我是关闭事件");
            backgroundAlpha(1f);
        }

    }
}
